package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.enums.space.ViewType;
import com.xhwl.common.pojo.cent.sdata.OrganizationType;
import com.xhwl.common.pojo.config.OrganizationConfig;
import com.xhwl.common.query.OrganizationConfigQuery;
import com.xhwl.common.query.cent.sdata.OrganizationTypeQuery;
import com.xhwl.data.dao.IOrganizationTypeDao;
import com.xhwl.data.service.IOrganizationConfigFeignService;
import com.xhwl.data.service.IOrganizationConfigService;
import com.xhwl.data.service.IOrganizationTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 14:26 2021/8/2
 */
@Service
public class OrganizationTypeServiceImpl extends ServiceImpl<IOrganizationTypeDao, OrganizationType> implements IOrganizationTypeService {

    @Autowired
    private IOrganizationTypeDao organizationTypeDao;

    @Autowired
    private IOrganizationConfigFeignService organizationConfigFeignService;

    @Override
    public List<OrganizationType> list(OrganizationTypeQuery organizationTypeQuery) {
        QueryWrapper<OrganizationType> organizationTypeQueryWrapper = new QueryWrapper<>();
        organizationTypeQueryWrapper.select("id", "name", "view_type", "code");
        queryHandler(organizationTypeQueryWrapper, organizationTypeQuery);
        return organizationTypeDao.selectList(organizationTypeQueryWrapper);
    }

    private void queryHandler(QueryWrapper<OrganizationType> organizationTypeQueryWrapper, OrganizationTypeQuery organizationTypeQuery) {
        if (null != organizationTypeQuery) {
            if (StringUtils.isNotEmpty(organizationTypeQuery.keywords)) {
                organizationTypeQueryWrapper.like("name", organizationTypeQuery.keywords);
            }
            if (StringUtils.isNotEmpty(organizationTypeQuery.code)) {
                organizationTypeQueryWrapper.eq("code", organizationTypeQuery.code);
            }
            if (CollectionUtils.isNotEmpty(organizationTypeQuery.viewType)) {
                organizationTypeQueryWrapper.in("view_type", organizationTypeQuery.viewType);
            }
            if (null != organizationTypeQuery.industryId) {
                OrganizationConfigQuery organizationConfigQuery = new OrganizationConfigQuery();
                List<OrganizationConfig> list = organizationConfigFeignService.list(organizationConfigQuery);
                if (CollectionUtils.isNotEmpty(list)) {
                    organizationTypeQueryWrapper.in("id", list.stream().map(l -> l.getType()).collect(Collectors.toSet()));
                } else {
                    organizationTypeQueryWrapper.in("id", -1);
                }
            }
        }
    }

    @Override
    public Page<OrganizationType> page(OrganizationTypeQuery organizationTypeQuery, Page page) {
        QueryWrapper<OrganizationType> organizationTypeQueryWrapper = new QueryWrapper<>();
        queryHandler(organizationTypeQueryWrapper, organizationTypeQuery);
        return organizationTypeDao.selectPage(page, organizationTypeQueryWrapper);
    }

	@Override
	public List<Integer> spaceIds() {
		QueryWrapper<OrganizationType> organizationTypeQueryWrapper = new QueryWrapper<>();
		organizationTypeQueryWrapper.select("id");
		organizationTypeQueryWrapper.in("view_type", Arrays.asList(ViewType.SPACE_DEFINED, ViewType.SPACE));
		List<OrganizationType> organizationTypes = organizationTypeDao.selectList(organizationTypeQueryWrapper);
		if(organizationTypes.isEmpty()){
			return Collections.emptyList();
		}
		return organizationTypes.stream().map(i -> i.getId()).collect(Collectors.toList());
	}
}
